//
//  UIBarButtonItem + Extension.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 12/27/15.
//  Copyright © 2015 RendezvousAuParadis. All rights reserved.
//

import UIKit


extension UIBarButtonItem {
    
    /**
    只有图片
    */
    convenience init(imageName imgName: String, target: AnyObject?, action: Selector) {
        self.init(imageName: imgName, title: nil, target: target, action: action)
    }
    
    /**
    只有文字
    */
    convenience init(title: String, target: AnyObject?, action: Selector) {
        
        self.init(imageName: nil, title: title, target: target, action: action)
    }
    
    /**
    图片 + 文字
    */
    convenience init(imageName imgName: String?, title: String?, target: AnyObject?, action: Selector) {
        let button = UIButton(type: .Custom)
        // 设置图片
        if let str = imgName {
            
            button.setImage(UIImage(named: str), forState: .Normal)
            button.setImage(UIImage(named: "\(str)_highlighted"), forState: .Highlighted)
        }
        
        if let str = title {
            // 设置文字
            button.setTitle(str, forState: .Normal)
            button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
            button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
            button.titleLabel?.font = UIFont.systemFontOfSize(14)
            
        }
        
        
        button.sizeToFit()
        
        // 添加动作方法
        button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        
        // 添加至自身
        self.init(customView: button)
    }
    
}


